# encoding: utf-8
# !/usr/local/bin/python3

"""
@author: Gao Shuo
@contact: dorothy400@163.com
@software: PyCharm
@file: clip_test_file.py
@time: 2020/5/14 10:11
没有人工标注的图片，全切
input: 
output: 
"""

import os.path
import sys
import cv2
from PIL import Image
import sys
import numpy as np
from functions import new_folder
Image.MAX_IMAGE_PIXELS = 1978138624

if __name__ == "__main__":
    path_ori = sys.argv[1]
    dest_ori_clip = new_folder(sys.argv[2])
    ext = ".jpg"
    ext_label = ".png"
    ori_imgs=[]
    label_imgs=[]
    target_width = 800
    target_height = 480
    li1 = os.listdir(path_ori)  # 谷歌……
    for path1 in li1:
        path1_os = os.path.join(path_ori, path1)
        jpg_names = os.listdir(path1_os)
        jpg_name = [x for x in jpg_names if ext in x and x.split(ext)[1] == ""]  # 图片
        if len(jpg_name) != 1:
            print "================不正常的目录================"
            print path1_os
        else:
            path2_os = os.path.join(path1_os, jpg_name[0])
            ori_imgs.append(path2_os)
    for path_ori in ori_imgs:
        im_ori = Image.open(path_ori)
        size = im_ori.size
        n_width = size[0] / target_width
        n_height = size[1] / target_height
        # print "============n_width" +str  (n_width)
        # print "============n_height" +str  (n_height)
        left, right = size[0] - target_width, size[0] # 剪切点
        top, bottom  = size[1] - target_height, size[1]
        # print "============top" +str  (top)
        # print "============bottom" +str  (bottom)
        filepath = path_ori.split('/')[-1:][0]
        for i in range(n_width+1):
            if i!=n_width:
                left,right =( target_width * i, target_width * (i+1))
            else:
                left, right = size[0] - target_width, size[0]  # 剪切点

            for j in range(n_height+1):
                if j!=n_height:
                    top ,bottom = (target_height * j, target_height * (j+1))
                else:
                    top, bottom = size[1] - target_height, size[1]
                image_ori = im_ori.crop((left, top, right, bottom))
                filename = "%s_%d_%d_%d_%d.png" % (filepath,left, top, right, bottom )
                image_ori.save(os.path.join(dest_ori_clip, filename))
                print "success! "+ filename















